package com.baidu.day06.Goods;

import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Goods goods = new Goods();
        GoodServices goodServices = new GoodServices();
        UserService userService = new UserService();
        System.out.println("=================欢迎使用超市管理系统==================");
        //提示用户进行操作
        System.out.println("请选择操作: 1.注册   2.登录  3.查看所有用户  4.退出系统");
        while (true) {
            Scanner sc = new Scanner(System.in);
            String userInput = sc.next();
            switch (userInput) {
                case UserConst.REGISTER:
                    userRegister(sc, userService);
                    break;
                case UserConst.LOGIN:
                    userLogin(sc, userService, goodServices);
                    break;
                case UserConst.FIND_ALL:
                    userService.findAllUser();
                    break;
                case SystemConst.EXIST:
                    System.out.println("欢迎下次使用");
                    System.exit(0);//退出JVM
            }
        }
    }

    //登录操作
    private static void userLogin(Scanner sc, UserService userService, GoodServices goodServices) {
        System.out.println("请输入账号:");
        String accountLogin = sc.next();
        System.out.println("请输入密码:");
        String passwordLogin = sc.next();
        User loginUser = userService.login(accountLogin, passwordLogin);
        if (loginUser == null) {
            System.out.println("登录失败,用户名或密码错误");
            return;
        } else {
            //对商品进行增删改查
            if (accountLogin.equals("admin")) {
                showAdminMenu(goodServices, loginUser);
            } else {
                showUserMenu(loginUser, sc, goodServices, userService);
            }
        }
    }

    //注册操作
    private static void userRegister(Scanner sc, UserService userService) {
        System.out.println("请输入用户的账号:");
        String account = sc.next();
        System.out.println("请输入用户的密码:");
        String password = sc.next();
        System.out.println("请确认密码:");
        String confirmPassword = sc.next();
        if (!password.equals(confirmPassword)) {
            System.out.println("两次输入的密码不一致");
            return;
        }
        int id = UserService.userList.size() + 1;
        User user = new User(id, account, password, 0.0);
        boolean flag = userService.register(user);
        System.out.println(flag ? "注册成功" : "注册失败");
    }

    //管理员菜单
    private static void showAdminMenu(GoodServices goodServices, User user) {
        while (true) {
            System.out.println("欢迎" + user.getAccount() + "回来");
            System.out.println("请输入您要进行的操作 1.查看商品   2.添加商品  3.删除商品  4.退出");
            Scanner sc = new Scanner(System.in);
            String userPut = sc.next();
            switch (userPut) {
                case GoodsConst.FIND_ALL:
                    System.out.println("查看商品");
                    goodServices.findAll();
                    break;
                case GoodsConst.ADD:
                    System.out.println("请输入你要添加的商品编号:");
                    Integer addNum = sc.nextInt();
                    System.out.println("请输入你要添加的商品名称:");
                    String addName = sc.next();
                    System.out.println("请输入你要添加的商品价格:");
                    Double addPrice = sc.nextDouble();
                    System.out.println("请输入你要添加的商品库存:");
                    Integer addStock = sc.nextInt();
                    Goods newGoods = new Goods(addNum, addName, addPrice, addStock);
                    boolean flag = GoodServices.addGoods(newGoods);
                    if (flag) {
                        System.out.println("添加成功");
                    } else {
                        System.out.println("添加失败");
                    }
                    break;
                case GoodsConst.REMOVE:
                    System.out.println("请输入你要删除商品的编号");
                    int inputNum = sc.nextInt();
                    GoodServices.deleteGoods(inputNum);
                    break;
                case SystemConst.EXIST:
                    System.exit(0); // 正常退出

            }

        }
    }

    //普通用户的操作
    private static void showUserMenu(User user, Scanner sc, GoodServices goodServices,UserService userService) {
        System.out.println("欢迎" + user.getAccount() + "回来");

        while (true) {
            System.out.println("请输入您要进行的操作 1.查看商品   2.购买商品  4.退出");
            String userPut = sc.next();
            switch (userPut) {
                case GoodsConst.FIND_ALL:
                    goodServices.findAll();
                    break;
                case "2":
                    System.out.println("请输入你要购买的商品名称:");
                    String buyName = sc.next();
                    System.out.println("请输入你要购买的商品数量:");
                    int buyNumber = sc.nextInt();

                    userService.buy( buyName,buyNumber);
                    break;
                case SystemConst.EXIST:
                    System.exit(0); // 正常退出
            }
        }
    }
}











